import { createRouter, createWebHistory } from 'vue-router'
import { useUserStore } from '@/stores'
import NProgress from 'nprogress'
import 'nprogress/nprogress.css'

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/login',
      name: 'login',
      component: () => import('../views/login/index.vue'),
      meta: {
        title: '登录'
      }
    },
    {
      path: '/user/patient',
      component: () => import('../views/user/patient.vue'),
      meta: {
        title: '家庭档案'
      }
    },
    {
      path: '/',
      redirect: '/home',
      component: () => import('../views/layout/index.vue'),
      children: [
        {
          path: '/home',
          component: () => import('../views/home/index.vue'),
          // 路由的元数据
          meta: {
            title: '首页'
          }
        },
        {
          path: '/article',
          component: () => import('../views/article/index.vue'),
          // 路由的元数据
          meta: {
            title: '健康百科'
          }
        },
        {
          path: '/notify',
          component: () => import('../views/notify/index.vue'),
          // 路由的元数据
          meta: {
            title: '消息通知'
          }
        },
        {
          path: '/user',
          component: () => import('../views/user/index.vue'),
          // 路由的元数据
          meta: {
            title: '个人中心'
          }
        }
      ]
    },
    {
      path: '/consult/fast',
      component: () => import('../views/consult/index.vue'),
      meta: {
        title: '极速问诊'
      }
    },
    {
      path: '/consult/dep',
      component: () => import('../views/consult/ConsultDep.vue'),
      meta: {
        title: '选择科室'
      }
    },
    {
      path: '/consult/illness',
      component: () => import('@/views/Consult/ConsultIllness.vue'),
      meta: { title: '病情描述' }
    },
    {
      path: '/consult/pay',
      component: () => import('@/views/Consult/ConsultPay.vue'),
      meta: { title: '问诊支付' }
    },
    {
      path: '/room',
      component: () => import('@/views/room/index.vue'),
      meta: { title: '问诊室' },
      // 进入路由页面之前的钩子函数
      beforeEnter: (to) => {
        if (to.query.payResult === 'false') return '/user/consult'
      }
    },
    {
      path: '/user/consult',
      component: () => import('@/views/user/ConsultPage.vue'),
      meta: { title: '问诊记录' }
    },
    {
      path: '/user/consult/:id',
      component: () => import('@/views/user/ConsultDetail.vue'),
      meta: { title: '问诊详情' }
    },
    {
      path: '/order/pay',
      component: () => import('@/views/order/OrderPay.vue'),
      meta: { title: '药品支付' }
    },
    {
      path: '/order/pay/result',
      component: () => import('@/views/order/OrderPayResult.vue'),
      meta: { title: '药品支付结果' }
    },
    {
      path: '/order/logistics/:id',
      component: () => import('@/views/order/OrderLogistics.vue'),
      meta: { title: '物流详情' }
    },
    {
      path: '/login/callback',
      component: () => import('@/views/login/LoginCallback.vue'),
      meta: { title: 'QQ登录-绑定手机' }
    }

    // {
    //   path: '/user',
    //   name: 'user',
    //   component: () => import('../views/user/index.vue')
    // }
  ]
})

// token 的跳转鉴权
// 1、获取token
// 2、判断当前是否有token以及去往的页面是否需要token
// 3、对于不需要登录即可以访问的页面 可以单独定义一个白名单数组 /login /register /404 。。。
router.beforeEach((to) => {
  NProgress.start()
  const { userinfo } = useUserStore()
  const whiteList = ['/login', '/login/callback']
  // 如果没有token 去的页面不是login 并且去的页面也不再白名单中
  if (!userinfo?.token && to.path !== '/login' && !whiteList.includes(to.path))
    return '/login'
})

router.afterEach((to) => {
  document.title = `优医问诊-${to.meta.title}`
  NProgress.done()
})

NProgress.configure({
  showSpinner: false
})

export default router
